package com.lwh.util;

import lombok.extern.slf4j.Slf4j;

import java.net.URL;
import java.net.URLDecoder;
import java.util.Properties;

/**
 * @Description:
 * @Date: 2019-10-14
 * @author: luowuhui
 */
@Slf4j
public class SystemUtil {


	// 取得本机信息
	public static String getLocalhostInfo() {
		StringBuffer sb    = new StringBuffer();
		Properties   props = System.getProperties(); // 获得系统属性集
		sb.append("操作系统:\n名称 os.name:");
		sb.append(props.getProperty("os.name"));
		sb.append("\n");
		sb.append("构架 os.arch:");
		sb.append(props.getProperty("os.arch"));
		sb.append("\n");
		sb.append("版本 os.version:");
		sb.append(props.getProperty("os.version"));
		sb.append("\n");
		URL url = FuncUtil.class.getProtectionDomain().getCodeSource().getLocation();
		try {
			sb.append("jar信息:\n");
			sb.append(URLDecoder.decode(url.getPath(),"utf-8"));
		} catch (Exception e) {
			log.error("", e);
		}
		return sb.toString();
	}

	//取得内存使用情况
	public static String getMemoryStatus() {
		StringBuffer sb = new StringBuffer();
		sb.append("\njvm内存使用情况\n最大可用内存:");
		sb.append(Runtime.getRuntime().maxMemory());
		sb.append("\n当前JVM空闲内存:");
		sb.append(Runtime.getRuntime().freeMemory());
		sb.append("\n当前JVM占用的内存总数:");
		sb.append(Runtime.getRuntime().totalMemory());
		sb.append("\n内存使用率:");
		sb.append((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) * 100 / Runtime.getRuntime().maxMemory());
		sb.append("%");
		return sb.toString();
	}
}
